package main.java.FSMDE.controllers;

import java.util.ArrayList;
import java.util.List;

import main.java.ERMDE.models.Diagram;
import main.java.FSMDE.controllers.BaseController;
import main.java.FSMDE.controllers.factories.IComponentControllerFactory;
import main.java.FSMDE.controllers.listeners.IInterfaceEventListener;
import main.java.FSMDE.infrastructure.IProjectContext;
import main.java.FSMDE.models.Component;
import main.java.FSMDE.models.ComponentCollection;
import main.java.FSMDE.models.Interface;
import main.java.FSMDE.models.Operations;
import main.java.FSMDE.views.IInterfaceView;


public class InterfaceController extends BaseController implements
		IInterfaceController {

	private List<IInterfaceEventListener> listeners;
	private Interface pendingInterface;
	private Iterable<Interface> interfaceCollection;
	private IInterfaceView interfaceView;
	private IComponentControllerFactory componentControllerFactory;
	private IComponentController componentController;
	private ArrayList<Diagram> ermDiagrams;
	private Operations operation;

	public InterfaceController(IProjectContext projectContext,
			IInterfaceView interfaceView,
			IComponentControllerFactory componentControllerFactory) {
		super(projectContext);
		this.pendingInterface = new Interface("");
		this.listeners = new ArrayList<IInterfaceEventListener>();
		this.interfaceCollection = projectContext
				.getAllInterfaces(this.pendingInterface);
		this.setInterfaceView(interfaceView);
		this.operation = Operations.None;
		this.componentControllerFactory = componentControllerFactory;
	}

	@Override
	public void create() {
		this.operation = Operations.Creating;
		this.interfaceView.setOperation(this.operation);
		this.interfaceView.setInterfaceName(this.pendingInterface.getName());
		this.componentController = this.componentControllerFactory.create();
		this.componentController.setErmDiagrams(ermDiagrams);
		this.interfaceView.addComponentView(componentController
				.getComponentView());
		this.interfaceView.showView();
	}

	@Override
	public void create(Interface interface0) {
		this.operation = Operations.Updating;
		this.componentController = this.componentControllerFactory.create();
		this.componentController.setErmDiagrams(ermDiagrams);
		this.interfaceView.setOperation(this.operation);
		this.interfaceView.addComponentView(componentController
				.getComponentView());
		this.pendingInterface = interface0;
		this.interfaceView.setInterfaceName(this.pendingInterface.getName());
		this.interfaceView.setInterfaceType(this.pendingInterface.getType());
		this.interfaceView.setInterfaceGraphType(this.pendingInterface
				.getGraphType());
		this.interfaceView.setComponents(this.pendingInterface.getComponents());
		this.interfaceView.setModeUpdating();
		this.interfaceView.showView();
	}

	public void setInterfaceView(IInterfaceView interfaceView) {
		this.interfaceView = interfaceView;
		this.interfaceView.setController(this);
	}

	@Override
	public void addSubscriber(IInterfaceEventListener listener) {
		this.listeners.add(listener);
	}

	@Override
	public boolean addInterface() {
		String name = this.interfaceView.getInterfaceName();
		if ((this.operation == Operations.Creating)
				&& (!this.validInterfaceName(name))) {
			return false;
		}
		this.pendingInterface.setName(name);
		this.pendingInterface.setType(this.interfaceView.getInterfaceType());
		this.pendingInterface.setGraphType(this.interfaceView.getGraphType());
		ComponentCollection comps = new ComponentCollection();
		for (Component comp : componentController.getComponents()) {
			comps.add(comp);
		}
		this.pendingInterface.setComponents(comps);
		for (IInterfaceEventListener listener : listeners) {
			listener.handleCreatedEvent(this.pendingInterface);
		}
		return true;
	}

	@Override
	public void cancelPendingInterfaceAction() {
		this.pendingInterface = null;
	}

	@Override
	public boolean validInterfaceName(String interfaceName) {
		if (interfaceName.equals("")) {
			return false;
		}
		for (Interface interface0 : interfaceCollection) {
			if (interface0.getName().equals(interfaceName)) {
				return false;
			}
		}
		return true;
	}

	@Override
	public void setErmDiagrams(ArrayList<Diagram> ermDiagrams) {
		this.ermDiagrams = ermDiagrams;
	}

}
